<template>
  <!-- 表单数据 -->
  <el-form ref="formData" :model="formData" :rules="rules" label-width="100px">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请输入聘用形式">
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input
        v-model="formData.departmentName"
        style="width:80%"
        placeholder="请输入部门"
        @focus="departmentNameFocus"
      />
      <div v-if="showTree" class="tree-box">
        <el-tree
          :data="treeData"
          :props="{ label: 'name' }"
          default-expand-all
          @node-click="treeClick"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" placeholder="请输入转正日期" style="width: 80%;" />
    </el-form-item>
    <el-form-item>
      <el-button @click="addCancel">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
// 导入我们枚举的方法  数据格式的转换
import EmployeeEnum from '@/constant/employees'

// 引入api // departments 方法
import { getDepartments } from '@/api/departments'

// 导入api方法
import { addEmployee } from '@/api/employees'

// 导入树形结构数据的转换方法
import { transTree } from '@/utils/index'

export default {
  name: 'EmpDialog',
  data() {
    return {

      hireType: EmployeeEnum.hireType, // 聘用形式数据绑定

      treeData: [], // 树形结构数据

      // 展示树形结构数据
      // defaultProps: {
      //   children: 'children',
      //   label: 'name'
      // },

      showTree: false, // 控制树形组件的隐藏和显示
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },

      // 添加表单校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' }
        ]
      }
    }
  },

  // created() {
  //   // 调用获取树形结构的数据
  //   this.getDepartments()
  // },
  methods: {
    // 表单重置数据
    resetForm() {
      //  把表单的数据项清空

      // 表单重置数据重置表单
      this.$refs.formData.resetFields()
    },

    // tree 节点被点击时的回调
    treeClick(data, node) {
      // alert(1)
      // 如果当前部门还有子部门，则不能被选中
      if (data && data.children.lenght) {
        return
      }

      // 把当前选中的节点显示在input 框中
      this.formData.departmentName = data.name
      // 隐藏tree
      this.showTree = false
    },

    // 当input 输入框获取到焦点时获取到树形数据
    departmentNameFocus() {
      this.getDepartments()
      // 显示树形控件
      this.showTree = true
    },

    // 获取树形结构的数据
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)

      this.treeData = transTree(res.data.depts)
    },

    // 点击确定按钮
    onSubmit() {
      this.$refs.formData.validate(async valid => {
        if (!valid) return

        const res = await addEmployee(this.formData)
        // console.log(res)
        this.$message.success(res.message)

        // 子传父组件添加成功的事件
        this.$emit('update')
      })
    },

    // 点击取消按钮
    addCancel() {
      this.$emit('close')
    }
  }
}
</script>
<style scoped lang="scss">
.el-form {
    padding: 0 0 0 40px;
}
</style>
